package cc.eddic.pratice.programmingparadigms.exam.impl;

import cc.eddic.pratice.programmingparadigms.exam.Gender;
import cc.eddic.pratice.programmingparadigms.exam.MasterDataService;
import cc.eddic.pratice.programmingparadigms.exam.domain.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class MasterDataServiceLxImpl implements MasterDataService {
    @Override
    public List<Category> getCategories() {
        Category software = new Category("软件开发基础");
        Category office = new Category("办公软件");
        Category code = new Category("代码");
        return new ArrayList<>(Arrays.asList(
                software.add(new SubCategory("基础")).add(new SubCategory("HTML"))
                        .add(new SubCategory("SQL")).add(new SubCategory("DOS"))
                        .add(new SubCategory("Linux")).add(new SubCategory("数学")),
                office.add(new SubCategory("Word")).add(new SubCategory("Excel")),
                code.add(new SubCategory("代码"))
        ));
    }

    @Override
    public List<Student> getStudents() {
        return new ArrayList<>(Arrays.asList(
                new Student("谷常生", Gender.MALE,"计算机"),
                new Student("李惠",Gender.FEMALE,"计算机"),
                new Student("谭雨晴",Gender.FEMALE,"通信"),
                new Student("李馨", Gender.FEMALE,"计算机"),
                new Student("曾奕哲",Gender.MALE,"计算机"),
                new Student("车晓满",Gender.MALE,"计算机"),
                new Student("王巧家",Gender.MALE,"交通"),
                new Student("孙晨玮",Gender.MALE,"土木工程"),
                new Student("崔相元",Gender.MALE,"计算机"),
                new Student("韩睿洁",Gender.MALE,"计算机")
                ));
    }

    @Override
    public List<Exam> getExams() {
//        如果找到了 isPresent() 使用 get() 取 Exam 对象
        List<Exam> examList=new ArrayList<>();
        Optional<Exam> optionalExam=find(examList, new Predicate<Exam>() {
            @Override
            public boolean test(Exam exam) {
//                按照什么条件搜索
                return false;
            }
        });
        if(optionalExam.isPresent()){
            Exam exam = optionalExam.get();
        }
//        没找到时，使用 orElseThrow 抛出异常
//        List<Student> studentList =new ArrayList<>();
//        Student student = find(studentList, new Predicate<Student>() {
//            @Override
//            public boolean test(Student student1) {
//                return "李馨".equals(student1.getName());
//            }
//        }).orElseThrow(new Supplier<IllegalArgumentException>() {
//            @Override
//            public IllegalArgumentException get() {
//                return new IllegalArgumentException("");
//            }
//        });
////        没找到时，使用 orElseGet() 返回默认值
//        List<Category> categoryList = new ArrayList<>();
//        Category category = find(categoryList, new Predicate<Category>() {
//            @Override
//            public boolean test(Category category) {
//                return false;
//            }
//        }).orElseGet(()->new Category("Default Category"));

        return new ArrayList<>(Arrays.asList(
                new Exam("7-31 写代码"),
                new Exam("8-15 理论"),
                new Exam("8-25 答辩")));


    }

    @Override
    public <T> Optional<T> find(List<T> genericList, Predicate<T> predicate) {
        for(T o:genericList){
            if(predicate.test(o)){
                return Optional.of(o);
            }
        }
        return Optional.empty();
    }
}
